// 课程表 3
console.log('scheduleCourse', Math.max(...[]), ...[])
function scheduleCourse(courses: number[][]): number {
    // 思路，判断课程最晚开始时间，按这个时间重新排序，逐个判断能不能上这门课
    // const LatestStartTimes: number[][] =  courses.map(item => {
    //     return [item[0], item[1] - item[0]]
    // }).filter(item => item[1] >= 0).sort((a,b) => b[1] - a[1])
    /** 从最短耗时或者最早完成切入都不能满足需求
    */
    // const fun1 = LatestStartTimes.sort((a,b) => a[1] - b[1])
    // const fun2 = LatestStartTimes.sort((a,b) => a[0] - b[0])
    // let usedTime = {
    //     fun1: 0,
    //     fun2: 0
    // }
    // let count = {
    //     fun1: 0,
    //     fun2: 0
    // }
    // fun1.forEach(item => {
    //     if (usedTime.fun1 <= item[1]) {
    //         usedTime.fun1 += item[0]
    //         count.fun1++ 
    //     }
    // })
    // fun2.forEach(item => {
    //     if (usedTime.fun2 <= item[1]) {
    //         usedTime.fun2 += item[0]
    //         count.fun2++ 
    //     }
    // })
    // console.log('count', count)
    // console.log('fun', fun1, fun2)

    /**  尝试从最晚完成向前推导
    */
    const LatestStartTimes: number[][] =  courses.map(item => {
        return [item[0], item[1] - item[0]]
    }).filter(item => item[1] >= 0)
    return reduce(0, 0, LatestStartTimes, '')
};

function reduce(time: number, count: number ,arr: number[][], path: string): number {
    console.log('reduce', time, count, arr, arr.length)
    const countArr = arr.map((item, index) => {
        if (time <= item[1] && arr.length){
            const p = path +'-'+ item.toString()
            console.log('map', path +'-'+ item.toString())
            return reduce(time + item[0], count + 1 , arr.filter((item, i) => i !== index), path +'-'+ item.toString())
        } else {
            console.log('count', count)
            return count
        }
    })
    console.log('max', countArr)
    if (countArr.length) return Math.max(...countArr)
    else return count
}

// const courses = [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]
// const courses = [[5,15],[3,19],[6,7],[2,10],[5,16],[8,14],[10,11],[2,19]]
// const courses = [[5,5],[4,6],[2,6]]
// const courses = [[5,15]]
// const courses = [[1,2],[2,3]]
const courses = [[1,2]]


console.log(scheduleCourse(courses))